int kbhit(void)
{
      struct timeval tv;
      fd_set read_fd;

        /* Do not wait at all, not even a microsecond */
      tv.tv_sec=0;
      tv.tv_usec=0;

      /* Must be done first to initialize read_fd */
      FD_ZERO(&read_fd);

      /* Makes select() ask if input is ready:
         * 0 is the file descriptor for stdin    */
      FD_SET(0,&read_fd);

      /* The first parameter is the number of the
         * largest file descriptor to check + 1. */
      if(select(1, &read_fd, NULL, NULL, &tv) == -1)
          return 0;
      if (FD_ISSET(0, &read_fd))
          return getchar();
      /* no characters were pending */
      return 0;
}
